package com.zl.codereview;

import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class MyTextGutterIconRenderer extends GutterIconRenderer {

    public final String myText;
    public final int myLineNumber;
    public final int status;
    public final String tooltipText;

    public MyTextGutterIconRenderer(String text,String content,int status, int lineNumber) {
        this.myText = text;
        this.myLineNumber = lineNumber;
        this.status = status;
        this.tooltipText = content;
        // 其他初始化代码...
    }

    @NotNull
    @Override
    public Icon getIcon() {
        return new CustomTextIcon(myText,status);
    }

    @Override
    public boolean isNavigateAction() {
        return false; // 根据需求决定是否支持点击跳转
    }

    @Nullable
    @Override
    public String getTooltipText() {
        return this.tooltipText;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof MyTextGutterIconRenderer)) {
            return false;
        }
        MyTextGutterIconRenderer that = (MyTextGutterIconRenderer) obj;
        return myText.equals(that.myText) && myLineNumber == that.myLineNumber;
    }

    @Override
    public int hashCode() {
        return 31 * myText.hashCode() + myLineNumber;
    }

    private static class CustomTextIcon implements Icon {
        private final String text;
        private final int status;

        public CustomTextIcon(String text,int status) {
            this.text = text;
            this.status = status;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            Graphics2D g2d = (Graphics2D) g.create();
            FontMetrics fm = g2d.getFontMetrics();
            int iconWidth = fm.stringWidth(text) + 4; // 假设左右各留2像素间距
            int iconHeight = fm.getHeight();

            int size = fm.getFont().getSize();
            g2d.setFont(new Font(null,Font.BOLD,size));
            if(status == 1) {
                g2d.setColor(JBColor.RED); // 文字颜色
            }else if(status == 2){
                g2d.setColor(new JBColor(new Color(6, 125, 23), new Color(6, 125, 23))); // 文字颜色
            }
            g2d.drawString(text, x + 2, y + fm.getAscent());
            g2d.dispose();
        }

        @Override
        public int getIconWidth() {
            return 8; // 根据实际需求计算宽度
        }

        @Override
        public int getIconHeight() {
            return 14; // 根据实际需求计算高度
        }
    }

}
